<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>收货单导入</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<jsp:include page="${pageContext.request.contextPath }/resource_path.jsp" flush="true"/>
</head>
<body class="layui-anim layui-anim-up">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a>
          <cite>收货单导入</cite>
        </a>
      </span>
    </div>
    <div class="x-body">
      <div style="margin-bottom: 18px">
		<div class="layui-inline">
			<table>
				<tr>
					<td>
						<input class="layui-input" name="receiveNumber" id="receiveNumberc" placeholder="请输入收货单编号" autocomplete="off" style="display: inline; width: 180px; margin-left: 10px">
					</td>
					<td>
						<!-- <input class="layui-input" name="sendCompany" id="sendCompanyc" placeholder="请输入供应商" autocomplete="off" style="display: inline; width: 180px; margin-left: 10px"> -->
						<!-- <div style="width: 190px; float: left;"> -->
						<div class="layui-form" style="width: 180px; margin-left: 10px">
							<select id="sendCompanyc" name="sendCompany">
							<!-- <select id="customer_id" name="customer_id" lay-verify="customer_id" lay-filter="customer_id" style="width: 190px"> -->
								<option value="">请选择供应商</option>
								<c:forEach items="${allcustomer}" var="customer">
									<option value="${customer.id}">${customer.customer_name}</option>
								</c:forEach>
							</select>
						</div>
					</td>
				</tr>
			</table>
		</div>
		<button class="layui-btn layui-btn-normal" data-type="reload"><i class="layui-icon layui-icon-search"></i>检索</button>
	  </div>
      <xblock>
			<button class="layui-btn layui-btn-warm" id="add"><i class="layui-icon layui-icon-add-circle-fine"></i>新增</button>
			<button class="layui-btn layui-btn-danger" id="dels"><i class="layui-icon layui-icon-delete"></i>批量删除</button>
      </xblock> 
	  <table class="layui-hide" id="tableList" lay-filter="tableList"></table>
	  <table class="layui-hide" id="tableDetails" lay-filter="tableDetails"></table>
	</div>
	
	<!-- 详情框 -->
	<div id="detailDiv" hidden>
		<table class="layui-hide" id="tableDetails" lay-filter="tableDetails"></table>
	</div>
	
	<script type="text/html" id="rowToolbar">
		<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
	</script>
	<script type="text/html" id="productAddRowToolbar">
		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="proDel">删除</a>
	</script>

	<!-- 导入框 -->
	<div id="importDiv" hidden="hidden">
		<form class="layui-form" id="uploadform">
			<div style="margin: 20px 0 20px 140px;">
				<input id="fileInput" name="file" type="file" accept=".xls,.xlsx"/>
			</div>
		</form>
		<button class="layui-btn layui-btn-blue" lay-filter="uplodeform" style="margin-left: 80px">导入数据</button>&emsp;&emsp;&emsp;&emsp;
		<button type="button" class="layui-btn" id="downloadFile">下载模板</button>
	</div>
	<!-- 产品选择框 -->
	<div id="productDiv" hidden="hidden">
		<form class="layui-form">
			<table id="productTable"></table>
            <button class="layui-btn layui-btn-blue" lay-submit lay-filter="productform" style="margin: 0px 0px 10px 200px; width: 500px">提交</button>
		</form>
	</div>

	<!-- 新增框 -->
	<div id="addDivID" hidden="hidden">
		<form class="layui-form" id="addFormID">
			<xblock>
				<table>
					<tr>
						<td>
							<label class="layui-form-label">收货单号</label></td>
						<td>
							<input class="layui-input" id="receiveNumber" readonly name="receiveNumber" style="width: 200px" placeholder="请输入收货单号">
							<input type="hidden" id="id" name="id">
						</td>
						<td>
							<label class="layui-form-label">供应商</label></td>
						<td>
						<div class="layui-input-inline" style="width:200px">
							<select class="layui-input" name="sendCompany" id="sendCompany"  lay-filter="sendCompany">
								<option value="">请选择供应商</option>
								<c:forEach items="${customers}" var="customer" >
									<option value="${customer.id}">${customer.customer_name}</option>
								</c:forEach>
							</select>
						</div>
                           <div class="layui-inline" style="margin-top: 10px;">
                               <span style="color:red; font-size: 24px; margin-left: 4px">*</span>
                           </div>
						<input type="hidden" id="text">
						</td>
						<td>
							<label class="layui-form-label">发货人</label></td>
						<td>
							<input class="layui-input" id="sendName" name="sendName" style="width: 200px">
						</td>
						
						<td>
							<label class="layui-form-label">发货日期</label></td>
						<td>
							<input class="layui-input" id="sendDate" name="sendDate" style="width: 200px" placeholder="请选择发货日期">
						</td>
					</tr>
					<tr style="height:20px">
					</tr>
				</table>
			</xblock>
			<table id="productAddTable" lay-filter="productAddTable"></table>
			<xblock>
				<div align="center">
					<button class="layui-btn layui-btn-blue" id="subBtn" lay-submit lay-filter="addForm">提交</button>
					&emsp;&emsp;&emsp;&emsp;
					<button class="layui-btn layui-btn-primary">取消</button>
				</div>
			</xblock>
		</form>
    </div>

	<script type="text/javascript">
		
        var table;
		layui.use(['table','layer','upload','form','laydate'], function(){
			table = layui.table;
			var layer = layui.layer;
			var form = layui.form;
			var laydate = layui.laydate;
			
			var $ = layui.jquery, active = {
				reload:function () {
					var receiveNumber = $.trim($("#receiveNumberc").val());
	                var sendCompany = $.trim($("#sendCompanyc").val());
					table.reload('contenttable',{
						method:'get',
						where:{
							receiveNumber:receiveNumber
	                        ,sendCompany:sendCompany
						}
						,page: {
		                    curr: 1//重新从第 1 页开始
		                }
					});
				}
			}
			
			$('.layui-btn').on('click', function(){
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});
			
			table.render({
				elem: '#tableList' 
				,url:'${pageContext.request.contextPath }/receive/list.do'
				,title: 'machineList'
				,id :'contenttable'
				,limits:[10,20,30]
				,cols: [
		                [{type: 'checkbox',fixed: 'left'},
		                 {field:'id',title:'NO',sort: true,width: 50,type:'numbers',align:'center'},
		                 {field: 'receiveNumber',title: '收货单编号',align:'center'},
		                // {field: 'sendNumber',title: '发货单编号',align:'center'},
		                 {field: 'sendCompany',title: '供应商',align:'center',},
		                 {field: 'sendName',title: '发货人',align:'center'},
		                 {field: 'sendDate',title: '发货日期',align:'center'},
		                 {field: 'receiveName',title: '收货人',align:'center'},
		                 {field: 'receiveDate',title: '收货日期',align:'center'},
		                 {field: 'state',title: '状态',align:'center'
		                    ,templet: function(d){
		                    	var value = d.state;
		                    	if(value == 0){
            	    				value = "未收货"
            	    			}else if(value == 1){
            	    				value = "已收货"
            	    			}else if(value == 2){
            	    				value = "已质检"
            	    			}else if(value == 3){
            	    				value = "已入库"
            	    			}else if(value == 4){
            	    				value = "已上架"
            	    			}else if(value == 5){
            	    				value = "已确认"
            	    			}
            	    			return value;
            	    		}
		                },
		               // {field: 'sureName', title: '确认人', align:'center'},
		                ,{fixed:'right',unresize: true, title:'操作', toolbar: '#rowToolbar', width:80, align: 'center'}
		                ]
		               ]
				,page: true
				,done : function(){
	                $('th').css({
	                    'background-color': '#009688', 'color': '#fff','font-weight':'bold'
	                })
	            }
			});
			
            
		 //监听行单击事件（单击事件为：rowDouble）
        table.on('row(tableList)', function(obj){
        	 var data = obj.data;
        	 table.render({
                 elem: '#tableDetails'
                 ,url:'${pageContext.request.contextPath }/receiveDetail/list.do?receiveNumber='+data.receiveNumber
                 ,toolbar: '#toolbar'
                 ,title: '明细表'
                 ,id :'contenttableDetails'
                 ,limits:[10,20,30]
                 ,defaultToolbar:['filter', 'print']
				 ,cols: [[
 					  {type: 'checkbox',fixed: 'left'},
 					  {field:'id', title:'序号', width: 50,type:'numbers', align:'center'},
                      {field:'mid', title:'物料id', align:'center',hide:true},
                      //{field:'customer_name', title:'供应商名称', align:'center'},
                      {field:'materiel_num', title:'产品码', align:'center'},
                      {field:'brevity_num', title:'简码', align:'center'},
                      {field:'materiel_name', title:'物料名称', align:'center'},
                      {field:'materiel_size', title:'物料规格', align:'center'},
                      {field:'materiel_properties', title:'物料型号', align:'center'},
                      {field:'unit', title:'单位', align:'center', width: 100},
                      {field:'packing_quantity', title:'包装数量', width: 100, align:'center'},
                      //{field:'container_name', title:'容器名称', align:'center'},
                      {field:'is_devanning', title:'是否拆箱', align:'center'
                    	  ,templet: function(d){
   	                    	var value = d.is_devanning;
   	                    	if(value == '0'){
   	                            return "是";
   	                        } else if (value == '1') {
   	                            return "否";
   	                        } 
                    	  }
                      },
                      {field:'devanning_num', title:'拆箱数', align:'center'},
                      {field:'outgoing_frequency', title:'出库频率', align:'center'},
                      {
 	                    field: 'barcode_type',
 	                    title: '条码类型',
 	                    align:'center'
                     	,templet: function(d){
 	                    	var value = d.barcode_type;
 	                    	if(value == '0'){
 	                            return "系统码";
 	                        } else if (value == '1') {
 	                            return "客户码1";
 	                        } else if (value == '2') {
 	                            return "客户码2";
 	                        } else {
 	                            return "无码";
 	                        }
            	    			return value;
            	    	}
 	                },
                      {field:'pici', title:'批次', align:'center'},
                      {field:'sendNum', title:'发货数量', align:'center'},
                    {field:'receiveNum', title:'收货数量', align:'center'}
    					  
                     ]]
                 ,page: true
                 ,done : function(){
 	                $('th').css({
 	                    'background-color': '#009688', 'color': '#fff','font-weight':'bold',
 	                    'font-size': 10,
 	                })
 	            }
             });
		});
			
			
        	table.render({
                elem: '#productAddTable'
                ,defaultToolbar:['filter', 'print']
                ,cols: [[
                	 {type: 'checkbox',fixed: 'left'},
					 {field:'id', title:'序号', width: 50,type:'numbers', align:'center'},
                     {field:'id', title:'id', align:'center',hide:true},
                     {field:'mid', title:'mid', align:'center',hide:true},
                     //{field:'customer_name', title:'供应商名称', align:'center',width:100},
                     {field:'materiel_num', title:'产品码', align:'center',width:100},
                     {field:'brevity_num', title:'简码', align:'center',width:100},
                     {field:'materiel_name', title:'物料名称', align:'center', width:100},
                     {field:'unit', title:'单位', align:'center',width:60},
                     {field:'packing_quantity', title:'包装数量', align:'center', width:100},
                     //{field:'container_name', title:'容器名称', align:'center'},
                     {field:'is_devanning', title:'是否拆箱', align:'center',width:80
                    	 ,templet: function(d){
    	                    	var value = d.is_devanning;
    	                    	if(value == '0'){
    	                            return "是";
    	                        } else if (value == '1') {
    	                            return "否";
    	                        } 
                     	  }
                     },
                     {field:'devanning_num', title:'拆箱数', align:'center',width:70},
                     {field:'outgoing_frequency', title:'出库频率', align:'center'},
                     {
	                    field: 'barcode_type',
	                    title: '条码类型',
	                    align:'center'
                    	,templet: function(d){
	                    	var value = d.barcode_type;
	                    	if(value == '0'){
	                            return "系统码";
	                        } else if (value == '1') {
	                        	
	                            return "客户码1";
	                        } else if (value == '2') {
	                            return "客户码2";
	                        } else {
	                            return "无码";
	                        }
           	    			return value;
           	    		}
   		             },
                     {field:'pici', title:'批次', edit: 'text', align:'center'},
                     {field:'sendNum', title:'发货数量', edit: 'text', align:'center'},
                     {field:'receiveNum', title:'收货数量', align:'center'}, 
				   					  
                    {fixed: 'right',title:'<a onclick="addProduct()"><i class="layui-icon layui-icon-search" style="font-size: 13px; color: #1E9FFF;"></i>查找带回</a>',toolbar: '#productAddRowToolbar',align:'center',unresize: true,width:125}
                ]]
            	,limit:Number.MAX_VALUE
    	        ,page: false
    	        ,height: 400
    	        ,done : function(){
	                $('th').css({
	                    'background-color': '#009688', 'color': '#fff','font-weight':'bold',
	                    'font-size': 10,
	                })
	            }
            });

			//监听行工具事件
			table.on('tool(productAddTable)', function(obj){
				var data = obj.data;
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				//单个删除
				if(obj.event === 'proDel'){
				  layer.confirm('确定删除吗？', function(index){
					  obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
					  layer.close(index);
				  });
				}
			});
			
			//监听行工具事件
			table.on('tool(tableList)', function(obj){
				var data = obj.data;
			
				if(obj.event === 'edit'){
					if(data.state != 0){
	            		toastr.warning("对不起，已收货的数据不能修改！");
	            		return;
	            	}
					layer.open({
					  type: 1 							//Page层类型
					  ,area: ['80%', '640px'] 	//宽  高
					  ,title: '编辑'
					  ,shade: 0.6 						//遮罩透明度
					  ,maxmin: true 					//允许全屏最小化
					  ,anim: 1 							//0-6的动画形式，-1不开启
					  ,content: $('#addDivID')
	                  ,end: function () {
	                      var formDiv = document.getElementById('addDivID');
	                      formDiv.style.display = '';
	                  }
	                  ,success: function(){
	                      $("#id").val(data.id)
	                      $("#receiveNumber").val(data.receiveNumber)
	                      $("#sendCompany").val(data.sendCompany)
	                      $("#sendName").val(data.sendName)
	                      $("#sendDate").val(data.sendDate)
                      	  var newData = new Array();
	                      $.ajax({
	                        type:'post',
	                        data:'receiveNumber='+data.receiveNumber,
	                        url:'${pageContext.request.contextPath}/receiveDetail/queryAllByMution.do',
	                        dataType: 'JSON',
	                        async: false,
	                        success:function (data) {
	                        	newData = data;
	                        }
	                      }); 
	                      table.reload('productAddTable',{
	        	              data : newData
	        	          });
	                      form.render();
	                    }
					});
				}else if(obj.event === 'detail') {
					layer.open({
					   type: 1 							//Page层类型
					   ,area: ['80%', '800px']			//宽  高
					   ,title: '详细'
					   ,shade: 0.6 						//遮罩透明度
					   ,maxmin: true 					//允许全屏最小化
					   ,anim: 1 							//0-6的动画形式，-1不开启
					   ,content: $('#detailDiv')
					   ,success: function () {
						   table.render({
				                elem: '#tableDetails'
				                ,url:'${pageContext.request.contextPath }/receiveDetail/list.do?receiveNumber='+data.receiveNumber
				                ,toolbar: '#toolbar'
				                ,title: '明细表'
				                ,id :'contenttableDetails'
				                ,limits:[10,20,30]
				                ,defaultToolbar:['filter', 'print']
				                ,cols: [
				                    [
									 {type: 'checkbox',fixed: 'left'},
									 {field:'id', title:'序号', width: 50,type:'numbers', align:'center'},
				                     {field:'id', title:'物料id', align:'center',hide:true},
				                     {field:'materiel_num', title:'产品码', align:'center'},
				                     {field:'brevity_num', title:'简码', align:'center'},
				                     {field:'materiel_name', title:'物料名称', align:'center'},
				                     {field:'unit', title:'单位', align:'center'},
				                     {field:'packing_quantity', title:'包装数量', align:'center'},
				                     {field:'container_name', title:'容器名称', align:'center'},
				                     {field:'is_devanning', title:'是否拆箱', align:'center'
				                    	 ,templet: function(d){
				    	                    	var value = d.is_devanning;
				    	                    	if(value == '0'){
				    	                            return "是";
				    	                        } else if (value == '1') {
				    	                            return "否";
				    	                        } 
				                     	  }
				                     },
				                     {field:'devanning_num', title:'拆箱数', align:'center'},
				                     {field:'outgoing_frequency', title:'出库频率', align:'center'},
				                     {
	        		                    field: 'barcode_type',
	        		                    title: '条码类型',
	        		                    align:'center'
	        		                    ,edit: 'text'
	    		                    	,templet: function(d){
	    			                    	var value = d.barcode_type;
	    			                    	if(value == '0'){
	    			                            return "系统码";
	    			                        } else if (value == '1') {
	    			                            return "客户码1";
	    			                        } else if (value == '2') {
	    			                            return "客户码2";
	    			                        } else {
	    			                            return "无码";
	    			                        }
	    		           	    			return value;
	    		           	    		}
	        		                },
				                     {field:'remark', title:'批次', align:'center'},
				                     {field:'sendNum', title:'发货数量', align:'center'},
				                   {field:'receiveNum', title:'收货数量', align:'center'}
				   					  
				                    ]]
				                ,page: true
				                ,done : function(){
					                $('th').css({
					                    'background-color': '#009688', 'color': '#fff','font-weight':'bold',
					                    'font-size': 10,
					                })
					            }
				            });
						   
				            
						 	//监听行单击事件（单击事件为：rowDouble）
					        table.on('row(tableDetails)', function(obj){
					        	 var data = obj.data;
					       		 table.render({
					                elem: '#tableCodes'
					                ,url:'${pageContext.request.contextPath }/receiveDetailCode/list.do?rdid='+data.id
					                ,toolbar: '#toolbar'
					                ,title: '产品信息表'
					                ,id :'contenttableCodes'
					                ,limits:[10,20,30]
					                ,defaultToolbar:['filter', 'print']
					                ,cols: [
					                    [
										 {type: 'checkbox',fixed: 'left'},
										 {field:'id', title:'序号', width: 50,type:'numbers', align:'center'},
					                     {field:'code', title:'条码',edit: 'text', align:'center',width:150},
					                     {field:'receiveNum', title:'数量',edit: 'text', align:'center'},
					                     {field:'pici', title:'批次', align:'center'},
					                     {field:'state', title:'状态',align:'center'},
					                     //{field:'remark', title:'备注',align:'center'}
					                    ]]
					                ,page: true
					                ,done : function(){
						                $('th').css({
						                    'background-color': '#009688', 'color': '#fff','font-weight':'bold',
						                    'font-size': 10,
						                })
						            }
					            });
					        });
					   }
					});
				}
			});

			//选择供应商选中id查询该供应商的所有物料
			form.on('select(sendCompany)', function(data){
				var customer_id = data.value;
				$.ajax({
                    type:'post',
                    data:'id='+customer_id,
                    url:'${pageContext.request.contextPath}/others/customerByName.do',
                    dataType: 'JSON',
                    async: false,
                    success:function (json) {
                    	if(null != json){
                    		$("#sendName").val(json.contacts_name)
                          	//$("#dpremark").val(json.contacts_tel)
                    	}
                    }
                }); 
			  	table.reload('productAddTable',{
	            	data : new Array()
	            }); 
			  	
			});      
			
			/**
			 * 添加弹框中，选择物料后，点提交，带回的操作
			 */
			form.on('submit(productform)', function (data) {
				var flag;
				var oldData = table.cache["productAddTable"];
				var checkStatus = table.checkStatus('productTable')
				//选中的物料
	      		var data = checkStatus.data;
				for (var j = 0; j < data.length; j++) {
					for (var k = 0; k < oldData.length; k++) {
						if (oldData[k].id == data[j].id) {
							flag = 0;
						}
					}
				}
				if (flag != 0) {
					for (var i = 0; i < data.length; i++) {
						oldData.push(data[i]);
					}
				} else {
					toastr.warning("记录已经存在，不能重复添加");
				}
				/* if (oldData.length == 0) {
					for (var i = 0; i < data.length; i++) {
						oldData.push(data[i]);
					}
				} else {
					//alert(data[i].id);
					//console.log(data[i].id);
					//alert(oldData.length);
					for (var j = 0; j < data.length; j++) {
						for (var k = 0; k < oldData.length; k++) {
							if (oldData[k].id == data[j].id) {
								toastr.warning("记录已经存在，不能重复添加");
							} else {
				            	oldData.push(data[j]);
							}
						}
					}
	            } */
	            table.reload('productAddTable',{
	            	data : oldData
	            });
	            layer.close(productIndex)
				return false;
			});
			
			
			/**
			 * 通用表单提交(AJAX方式)(新增)
			 */
			/* form.on('submit(productform)', function (data) {
				var oldData =  table.cache["productAddTable"];
				var checkStatus = table.checkStatus('productTable')
	      		var data = checkStatus.data;
				var num = 0;
				for (var i = 0; i < data.length; i++) {
					var ii = 0
					for (var j = 0; j < oldData.length; j++) {
						if(oldData[j].materiel.mid == data[i].mid){
							ii++;
							break;
						}
					}
					if(ii == 0){
						var datan = {materiel:data[i],num:0}
		            	oldData.push(datan);
					}else{
						num++;
					}
	            }
	            table.reload('productAddTable',{
	            	data : oldData
	            });
	            layer.close(productIndex)
	            if(num > 0){
	            	toastr.warning("有"+num+"条数据重复不能添加");
				}
				return false;
			}); */
			
			
			/**
			 * 新增--提交
			 */
			form.on('submit(addForm)', function (data) {
				
				
				var receiveNumber = $("#receiveNumber").val();
				
				//获取明细中数据
				var oldData = table.cache["productAddTable"];
				var data = oldData.filter(function(item){
					 var re = /^[1-9]\d*$/ ;
				     return item.sendNum == "" || item.sendNum == 0 || item.sendNum == undefined || !re.test(item.sendNum) || item.pici == "" || item.pici == null || item.pici == undefined; 
				})
				if(oldData.length == 0){
					 toastrStyle();
					 toastr.warning("请添加物料详情！");
					 return false;
					//alert("请添加详情！")
				}else if(data.length > 0){
					 toastrStyle();
					 toastr.warning("请输入正确的批次号和发货量(大于0的正整数)");
					 return false;
					//alert("有"+data.length+"条数据不正确,请输入正确的发货量(大于0的正整数)且批次号为必输项")
				}else{
				//获取明细表中的所有记录，遍历，循环添加
					for (var i = 0; i < oldData.length; i++) {
						var id = oldData[i].id;
						var mid = oldData[i].id;
						var sendNum = oldData[i].sendNum;
						var receiveNum = oldData[i].receiveNum;
						var pici = oldData[i].pici;
			            	//添加收货明细
						   $.ajax({
		                    url: '${pageContext.request.contextPath}/receiveDetail/insert.do' ,
		                    type: 'post',
		                    data: {receiveNumber:receiveNumber, mid:mid, id:id, sendNum:sendNum,receiveNum:receiveNum,pici:pici},
		                    async: false,
		                    dataType:'JSON',
		                    success: function (result) {
		                        if (result > 0) {
		                        	if(i == oldData.length - 1) {
		                        		var title = "新增";
			        					var url = '${pageContext.request.contextPath}/receive/insert.do';
			        					var id = $.trim($("#id").val());
			        					if(id != "" && id != null && id != 'undefined'){
			        						var url = '${pageContext.request.contextPath}/receive/update.do';
			        						title = "修改";
			        					}
			        				
			        		            //添加收货单
			        					$.ajax({
			        						url : url,
			        						data: $("#addFormID").serialize(),
			        						cache : false,
			        						async: false,
			        						type : "post",
			        						}).done(
			        							function(res) {
			        								if (res > 0) {
			        									toastr.success(title+'成功');
			        									setTimeout(function(){  
			        		                                window.location.reload();
			        		                                $("#reset").click();
			        		                            },1000); 
			        								}
			        					     	}
			        						).fail(
			        							function(res) {
			        								toastr.error(title+'失败！');
			        								setTimeout(function(){
			        									location.reload();
			        								},1000);
			        							}
			        						) 
		                        	}
		                        	
		                        }else{
		                            toastr.warning("对不起，明细添加失败！");
		                        }
		                    },
		                    error: function (data) {
		                        toastr.error("对不起，明细添加错误！");
		                    }
		                })
					}
		                return false;
					}
			});

			/**
			 * 通用表单提交(AJAX方式)(修改)
			 */
			/* form.on('submit(editForm)', function (data) {
				$.ajax({
					url : '${pageContext.request.contextPath}/controlPlan/editReactionPlanById.do',
					data: $("#editFormID").serialize(),
					cache : false,
					type : "post",
					}).done(
						function(res) {
							if (res > 0) {
								toastr.success('修改成功！');
								setTimeout(function(){
									location.reload();
								},1000);
							}
						}
					).fail(
						function(res) {
							toastr.error('修改失败！');
							setTimeout(function(){
								location.reload();
							},1000);
						}
					)
				return false;
			}); */

			
			//增加
            $("#add").click(function(){
                layer.open({
                    type: 1 					//Page层类型
                    ,area: ['80%', '640px'] 	//宽  高
                    ,title: '新增'
                    ,shade: 0.6 				//遮罩透明度
                    ,maxmin: true 				//允许全屏最小化
                    ,anim: 1 					//0-6的动画形式，-1不开启
                    ,content: $('#addDivID')
                    ,end: function () {
                        var formDiv = document.getElementById('addDivID');
                        formDiv.style.display = '';
                    }
                    ,success: function(){
                    	var timestamp = (new Date()).getTime();
                    	
                    	$("#receiveNumber").val("R-"+timestamp);
                    	
                    	laydate.render({
                            elem: '#sendDate'
                        });
                    	
                    	$("#sendDate").val(curentTime())
                    	table.reload('productAddTable',{
        	            	data : new Array()
        	            });
                    	
                    	$("#sendCompany").val("")
                        form.render();
                    }
                });
			});

          	//批量删除
			$("#dels").click(function(){
				var checkStatus = table.checkStatus('contenttable')
	      		var data = checkStatus.data;
	            var idArr = new Array();
	            if(data.length==0){
	                toastr.warning("请至少选择一条记录！");
	            }else{
	            	
	                var ids = "";
	                for(var i=0;i<data.length;i++){
	                	
	                	if(data[i].state != 0){
	                		toastr.warning("对不起，已收货的数据不能删除！");
	                		return;
	                	}
	                	
	                    if(i == 0 || i == "0"){
	                        ids += data[i].id
	                    }else{
	                        ids += "," + data[i].id;
	                    }
	                }
	                layer.confirm('确定删除吗？', function(index){
	                    $.ajax({
	                        type:'post',
	                        url:'${pageContext.request.contextPath }/receive/delete.do',
	                        data:{"ids":ids},
	                        success:function(data){
	                            if(data>0){
	                                toastr.success("删除成功！");
	                                setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
	                                    //关闭模态框
	                                    // 父页面刷新
	                                    window.location.reload();
	                                },2000);
	                            }else{
	                                toastr.warning("删除失败！");
	                            }
	                        }

	                    })
	                    layer.closeAll();
	                });
	            }
			});
		});
		
		function addProduct (){
			productIndex = layer.open({
                type: 1 					//Page层类型
                ,area: ['900px', '450px'] 	//宽  高
                ,title: '物料清单'
                ,shade: 0.6 				//遮罩透明度
                ,maxmin: true 				//允许全屏最小化
                ,anim: 1 					//0-6的动画形式，-1不开启
                ,content: $('#productDiv')
                ,end: function () {
                    var formDiv = document.getElementById('productDiv');
                    formDiv.style.display = '';
                }
                ,success: function () {
                	
                	var customer_id = $("#sendCompany").val();
                	table.render({
                        elem: '#productTable'
                        ,url:'${pageContext.request.contextPath }/receiveDetail/materielList.do?id='+customer_id
                        ,limits:[10,20,30]
                        ,defaultToolbar:['filter', 'print']
                        ,request: {   //如果无需自定义请求参数，可不加该参数
        	                pageName: 'page' //页码的参数名称，默认：page
        	                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        	            }
        	            ,cols: [
        		                [{
        		                    type: 'checkbox',
        		                    fixed: 'left'
        		                }, {
        		                	field:'id',
        		                	title:'序号', 
        		                	sort: true,
        		                	width: 60,
        		                	type:'numbers',
        		                	align:'center'
        		                }, {
        		                    field: 'id',
        		                    title: 'id',
        		                    hide:true 
        		                } , {
        		                    field: 'customer_name',
        		                    title: '供应商名称',
        		                    align:'center',
        		                }, {
        		                    field: 'materiel_num',
        		                    title: '产品码',
        		                    align:'center',
        		                } , {
        		                    field: 'brevity_num',
        		                    title: '简码',
        		                    align:'center',
        		                }, {
        		                    field: 'materiel_name',
        		                    title: '物料名称',
        		                    align:'center',
        		                }, {
        		                    field: 'outgoing_frequency',
        		                    title: '出库频率',
        		                    align:'center',
        		                }, {
        		                    field: 'container_name',
        		                    title: '容器名称',
        		                    align:'center'
        		                }, {
        		                    field: 'packing_quantity',
        		                    title: '包装数量',
        		                    align:'center',
        		                }, {
        		                    field: 'is_devanning',
        		                    title: '是否拆箱',
        		                    align:'center'
      		                    	 ,templet: function(d){
		    	                    	var value = d.is_devanning;
		    	                    	if(value == '0'){
		    	                            return "是";
		    	                        } else if (value == '1') {
		    	                            return "否";
		    	                        } 
		                     	  }
        		                }, {
        		                    field: 'barcode_type',
        		                    title: '条码类型',
        		                    align:'center'
    		                    	,templet: function(d){
    			                    	var value = d.barcode_type;
    			                    	if(value == '0'){
    			                            return "系统码";
    			                        } else if (value == '1') {
    			                            return "客户码1";
    			                        } else if (value == '2') {
    			                            return "客户码2";
    			                        } else {
    			                            return "无码";
    			                        }
    		           	    			return value;
    		           	    		}
        		                }
        		                ]
        		               ]
                        ,page: true
                    });
                }
            });
		}
		
		//获取当前时间
	   	function curentTime() {
	   		var now=new Date();
	   		var year = now.getFullYear();       //年
	   	    var month = now.getMonth() + 1;     //月
	   	    var day = now.getDate();            //日
	   	    var time=year+"-"+add0(month)+"-"+add0(day);
	   		return time;
	   	}
	   	function add0(m){return m<10?'0'+m:m }
		toastrStyle()
	</script>
</body>
</html>
